package com.d3.pomodoro.activity

import android.os.Build
import android.os.Bundle
import com.d3.pomodoro.R
import com.d3.pomodoro.config.HawkConfig
import com.d3.pomodoro.fragment.MainFragment
import com.orhanobut.hawk.Hawk
import com.ramotion.paperonboarding.PaperOnboardingFragment
import com.ramotion.paperonboarding.PaperOnboardingPage


open class MainActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        if (Hawk.get(HawkConfig.WELCOME_KEY, false)) {
            pageOnBoarding()
        } else {
            openMain()
        }
    }

    private fun pageOnBoarding() {
        changeStatusBarColor(R.color.pink1)
        val scr1 = PaperOnboardingPage().apply {
            titleText = getString(R.string.onboard1)
            descriptionText = getString(R.string.onboardtips1)
            bgColor = resources.getColor(R.color.pink1)
            contentIconRes = R.mipmap.guide1
            bottomBarIconRes = R.mipmap.icon_onboard
        }
        val scr2 = PaperOnboardingPage().apply {
            titleText = getString(R.string.onboard2)
            descriptionText = getString(R.string.onboardtips2)
            bgColor = resources.getColor(R.color.pink2)
            contentIconRes = R.mipmap.guide2
            bottomBarIconRes = R.mipmap.icon_onboard
        }
        val scr3 = PaperOnboardingPage().apply {
            titleText = getString(R.string.onboard3)
            descriptionText = getString(R.string.onboardtips3)
            bgColor = resources.getColor(R.color.pink3)
            contentIconRes = R.mipmap.guide3
            bottomBarIconRes = R.mipmap.icon_onboard
        }
        val scr4 = PaperOnboardingPage().apply {
            titleText = getString(R.string.onboard4)
            descriptionText = getString(R.string.onboardtips4)
            bgColor = resources.getColor(R.color.pink4)
            contentIconRes = R.mipmap.guide4
            bottomBarIconRes = R.mipmap.icon_onboard

        }
        val elements = ArrayList<PaperOnboardingPage>()
        elements.add(scr1)
        elements.add(scr2)
        elements.add(scr3)
        elements.add(scr4)

        val onBoardingFragment = PaperOnboardingFragment.newInstance(elements)

        onBoardingFragment.setOnRightOutListener {
            Hawk.put(HawkConfig.WELCOME_KEY, true)
            openMain()
        }

        onBoardingFragment.setOnChangeListener { p0, p1 ->
            when (p1) {
                0 -> {
                    changeStatusBarColor(R.color.pink1)
                }
                1 -> {
                    changeStatusBarColor(R.color.pink2)
                }
                2 -> {
                    changeStatusBarColor(R.color.pink3)
                }
                3 -> {
                    changeStatusBarColor(R.color.pink4)
                }
            }
        }

        supportFragmentManager.beginTransaction()
            .add(R.id.fragmentContainer, onBoardingFragment, "onBoarding").commit()
    }

    private fun openMain() {
        val fragmentTransaction = supportFragmentManager.beginTransaction()
        val bf = MainFragment()
        fragmentTransaction.replace(R.id.fragmentContainer, bf, "mainFragment").commit()
    }
}
